<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>积分排行榜 - LinkUp连连看</title>
    <link rel="stylesheet" th:href="@{/static/css/common.css}">
    <style>
        .rank-container {
            max-width: 1200px;
            margin: 80px auto 40px;
            padding: 0 20px;
        }

        .rank-header {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 30px;
            border-radius: 15px;
            margin-bottom: 30px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            text-align: center;
        }

        .rank-header h1 {
            margin: 0 0 10px 0;
            font-size: 32px;
        }

        .my-rank {
            font-size: 20px;
            font-weight: bold;
            margin-top: 15px;
        }

        .rank-table-container {
            background: white;
            border-radius: 15px;
            padding: 30px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
            margin-bottom: 30px;
        }

        .rank-table {
            width: 100%;
            border-collapse: collapse;
        }

        .rank-table thead {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }

        .rank-table th {
            padding: 15px;
            text-align: left;
            font-weight: 600;
        }

        .rank-table th:first-child {
            border-radius: 10px 0 0 0;
            text-align: center;
            width: 80px;
        }

        .rank-table th:last-child {
            border-radius: 0 10px 0 0;
        }

        .rank-table tbody tr {
            border-bottom: 1px solid #f0f0f0;
            transition: all 0.3s ease;
        }

        .rank-table tbody tr:hover {
            background: #f8f9ff;
        }

        .rank-table td {
            padding: 15px;
        }

        .rank-number {
            text-align: center;
            font-size: 20px;
            font-weight: bold;
            width: 80px;
        }

        .rank-medal {
            font-size: 28px;
        }

        .rank-1 { color: #FFD700; }
        .rank-2 { color: #C0C0C0; }
        .rank-3 { color: #CD7F32; }

        .user-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .user-avatar {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            object-fit: cover;
            border: 2px solid #667eea;
        }

        .user-details {
            flex: 1;
        }

        .user-nickname {
            font-weight: 600;
            color: #333;
            font-size: 16px;
        }

        .user-account {
            color: #999;
            font-size: 13px;
        }

        .score-value {
            font-size: 22px;
            font-weight: bold;
            color: #667eea;
        }

        .stats-group {
            display: flex;
            gap: 15px;
            font-size: 14px;
        }

        .stat-item {
            color: #666;
        }

        .stat-value {
            font-weight: 600;
            color: #333;
        }

        .win-rate {
            font-size: 16px;
            font-weight: 600;
        }

        .win-rate.high {
            color: #67c23a;
        }

        .win-rate.medium {
            color: #e6a23c;
        }

        .win-rate.low {
            color: #f56c6c;
        }

        .loading {
            text-align: center;
            padding: 60px 20px;
            font-size: 18px;
            color: #718096;
        }

        .empty-rank {
            text-align: center;
            padding: 80px 20px;
            color: #a0aec0;
        }

        .empty-rank-icon {
            font-size: 80px;
            margin-bottom: 20px;
        }

        .back-btn {
            display: inline-block;
            margin-top: 20px;
            padding: 12px 30px;
            background: white;
            color: #667eea;
            border: 2px solid #667eea;
            border-radius: 8px;
            text-decoration: none;
            font-weight: bold;
            transition: all 0.3s ease;
        }

        .back-btn:hover {
            background: #667eea;
            color: white;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .rank-table {
                font-size: 14px;
            }

            .stats-group {
                flex-direction: column;
                gap: 5px;
            }

            .user-avatar {
                width: 40px;
                height: 40px;
            }

            .score-value {
                font-size: 18px;
            }
        }
    </style>
</head>
<body>
    <nav class="navbar">
        <div class="nav-container">
            <div class="nav-logo">🎮 LinkUp 连连看</div>
            <div class="nav-menu">
                <a th:href="@{/user/home}">首页</a>
                <a th:href="@{/user/rank}" class="active">排行榜</a>
                <a th:href="@{/user/shop}">道具商店</a>
                <a href="#" onclick="logout()">退出</a>
            </div>
        </div>
    </nav>

    <div class="rank-container">
        <div class="rank-header">
            <h1>🏆 积分排行榜</h1>
            <p>谁是最强王者？挑战巅峰，成就传奇！</p>
            <div class="my-rank" id="myRankDisplay">
                我的排名：加载中...
            </div>
        </div>

        <div id="loadingMessage" class="loading">
            ⏳ 加载中...
        </div>

        <div id="rankTableContainer" class="rank-table-container" style="display: none;">
            <table class="rank-table">
                <thead>
                    <tr>
                        <th>排名</th>
                        <th>玩家</th>
                        <th>总积分</th>
                        <th>游戏统计</th>
                        <th>胜率</th>
                        <th>最大连击</th>
                    </tr>
                </thead>
                <tbody id="rankTableBody">
                    <!-- 动态加载排行榜数据 -->
                </tbody>
            </table>
        </div>

        <div id="emptyMessage" class="empty-rank" style="display: none;">
            <div class="empty-rank-icon">📊</div>
            <h2>暂无排行数据</h2>
            <p>快去玩游戏，成为第一个上榜的玩家吧！</p>
        </div>

        <div style="text-align: center;">
            <a th:href="@{/user/home}" class="back-btn">返回首页</a>
        </div>
    </div>

    <footer class="footer">
        <div class="footer-content">
            <p>&copy; 2025 LinkUp 连连看. All rights reserved.</p>
            <p>享受游戏，挑战自我！</p>
        </div>
    </footer>

    <script>
        // 加载排行榜数据
        function loadRankList() {
            fetch('/user/rank/list?limit=100')
                .then(response => response.json())
                .then(data => {
                    if (data.code === 200) {
                        const rankData = data.data;
                        const rankList = rankData.rankList || [];
                        const myRank = rankData.myRank || 0;

                        // 显示我的排名
                        document.getElementById('myRankDisplay').textContent = 
                            '我的排名：第 ' + myRank + ' 名';

                        if (rankList.length === 0) {
                            showEmpty();
                        } else {
                            renderRankTable(rankList);
                        }
                    } else {
                        alert('加载排行榜失败：' + data.message);
                        showEmpty();
                    }
                })
                .catch(error => {
                    console.error('加载失败', error);
                    alert('网络错误，请稍后重试');
                    showEmpty();
                });
        }

        // 渲染排行榜表格
        function renderRankTable(rankList) {
            const tbody = document.getElementById('rankTableBody');
            tbody.innerHTML = '';

            rankList.forEach(rank => {
                const row = document.createElement('tr');
                
                // 排名列
                const rankCell = document.createElement('td');
                rankCell.className = 'rank-number';
                if (rank.rankPosition <= 3) {
                    const medal = rank.rankPosition === 1 ? '🥇' : 
                                  rank.rankPosition === 2 ? '🥈' : '🥉';
                    rankCell.innerHTML = `<span class="rank-medal">${medal}</span>`;
                } else {
                    rankCell.textContent = rank.rankPosition;
                }
                row.appendChild(rankCell);

                // 玩家信息列
                const userCell = document.createElement('td');
                userCell.innerHTML = `
                    <div class="user-info">
                        <img src="${rank.avatar || '/static/images/default-avatar.png'}" 
                             alt="头像" class="user-avatar">
                        <div class="user-details">
                            <div class="user-nickname">${rank.nickname}</div>
                            <div class="user-account">${rank.account}</div>
                        </div>
                    </div>
                `;
                row.appendChild(userCell);

                // 总积分列
                const scoreCell = document.createElement('td');
                scoreCell.innerHTML = `<div class="score-value">${rank.totalScore}</div>`;
                row.appendChild(scoreCell);

                // 游戏统计列
                const statsCell = document.createElement('td');
                statsCell.innerHTML = `
                    <div class="stats-group">
                        <div class="stat-item">总场: <span class="stat-value">${rank.gameCount}</span></div>
                        <div class="stat-item">胜: <span class="stat-value">${rank.winCount}</span></div>
                        <div class="stat-item">负: <span class="stat-value">${rank.loseCount}</span></div>
                    </div>
                `;
                row.appendChild(statsCell);

                // 胜率列
                const winRateCell = document.createElement('td');
                const winRate = rank.winRate || 0;
                const rateClass = winRate >= 70 ? 'high' : winRate >= 40 ? 'medium' : 'low';
                winRateCell.innerHTML = `<div class="win-rate ${rateClass}">${winRate}%</div>`;
                row.appendChild(winRateCell);

                // 最大连击列
                const comboCell = document.createElement('td');
                comboCell.innerHTML = `<div class="stat-value">${rank.maxCombo}</div>`;
                row.appendChild(comboCell);

                tbody.appendChild(row);
            });

            document.getElementById('loadingMessage').style.display = 'none';
            document.getElementById('rankTableContainer').style.display = 'block';
        }

        // 显示空状态
        function showEmpty() {
            document.getElementById('loadingMessage').style.display = 'none';
            document.getElementById('emptyMessage').style.display = 'block';
        }

        // 退出登录
        function logout() {
            if (confirm('确定要退出登录吗？')) {
                window.location.href = '/user/logout';
            }
        }

        // 页面加载时初始化
        window.onload = function() {
            loadRankList();
        };
    </script>
</body>
</html>

